//
//  TelecontrolBrandViewController.m
//  NuwaRobot
//
//  Created by Kendama on 2017/12/4.
//  Copyright © 2017年 Kendama. All rights reserved.
//

#import "TelecontrolBrandViewController.h"
#import "SexTableViewCell.h"
#import "InfraredApi.h"
#import "NSDictionary+SetNullStr.h"
#import "InfraredBrandModel.h"

@interface TelecontrolBrandViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) NSMutableArray *dataSource;
@property(nonatomic,assign) NSInteger selectedIndex;

@end

@implementation TelecontrolBrandViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self getInfraredBrandList];
}

- (void)getInfraredBrandList
{
    if (!self.type)
    {
        return;
    }
    
    InfraredApi *api = [[InfraredApi alloc] initWithDic:@{@"appid":InfraredAppId,@"c":@"f",@"f":EXRACT_OBJECT(InfraredDeviceID),@"m":@"none",@"t":self.type}];
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [self.dataSource removeAllObjects];
        NSDictionary *dict = [NSDictionary dictionaryWithJsonString:request.responseString];
        [self.dataSource addObjectsFromArray:[NSArray yy_modelArrayWithClass:[InfraredBrandModel class] json:dict[@"rs"]]];
        self.selectedIndex = -1;
        [self.tableView reloadData];
        
    }];
}

#pragma mark --- tabelview代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return px1920Height(150);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SexTableViewCell *cell = [SexTableViewCell dequeueReusableWithTableView:tableView];
    InfraredBrandModel *model = self.dataSource[indexPath.row];
    cell.titleLabel.text = model.name;
    if (indexPath.row == self.selectedIndex)
    {
        cell.selectedButton.selected = YES;
    }
    else
    {
        cell.selectedButton.selected = NO;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    self.selectedIndex = indexPath.row;
    InfraredBrandModel *model = self.dataSource[indexPath.row];
    if (self.brandBlock)
    {
        self.brandBlock(model);
    }
    [self.tableView reloadData];
}


#pragma mark --- 懒加载
- (UITableView *)tableView
{
    if(!_tableView)
    {
        UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        [superView addSubview:_tableView];
        _tableView.backgroundColor = [UIColor clearColor];
    }
    return _tableView;
}

- (NSMutableArray *)dataSource
{
    if(!_dataSource)
    {
        _dataSource = [[NSMutableArray alloc]initWithArray:@[]];
    }
    return _dataSource;
}


#pragma mark --- 设置视图布局
- (void)masLayoutSubview
{
    __weak UIView *superView = self.view;
    
    
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(px1920Height(252));
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(40));
        make.right.mas_equalTo(superView.mas_right).with.offset(-px1080Width(40));
        make.bottom.mas_equalTo(superView.mas_bottom);
    }];
    
    
}


#pragma mark --- 设置导航栏
- (void)setNavigationStyle
{
    [self setNavigationBarTitle:@"品牌"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
